﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.Metadata.Entity
{
    /// <summary>
    /// Action元数据
    /// </summary>
    [Serializable]
    public class ActionMetaData: IMetaDataContent
    {
        public string ID { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        /// <summary>
        /// 是否为客户端Action
        /// </summary>
        /// <remarks>如果是客户端Action，需要维护具体的脚本信息</remarks>
        public bool IsClientAction { get; set; }
        /// <summary>
        /// Action类型
        /// </summary>
        public ActionType ActionType { get; set; }
        /// <summary>
        /// 当ActionType为CDO时，RelatedMetaDataID为CDO的ID
        /// 当ActionType为SQL时，RelatedMetaDataID为数据库的ID
        /// </summary>
        public string RelatedMetaDataID { get; set; }
        /// <summary>
        /// 当ActionType为SQL时，RelateMetaDataName为数据库的名称，界面显示用
        /// </summary>
        public string RelatedMetaDataName { get; set; }

        /// <summary>
        /// Action返回值类型
        /// </summary>
        public ReturnValueType ReturnValueType { get; set; }
        /// <summary>
        /// 是否为功能类Action，如果为True，可以作为DQ使用，需要对返回内容的Schema进行描述
        /// </summary>
        ///public bool IsFunction { get; set; }
        /// <summary>
        /// 返回值是否是列表
        /// </summary>
        ///public bool IsList { get; set; }
        /// <summary>
        /// 是否公共Action,暂时弃用,通过ACL列表进行访问控制
        /// </summary>
        /// <value>
        /// True:当前应用下共享
        /// False：当前Project私有
        /// </value>
        public bool IsPublic { get; set; }
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnable { get; set; }
        /// <summary>
        /// 是否启用缓存
        /// </summary>
        public bool IsCache { get; set; }
        /// <summary>
        /// 缓存有效期，单位毫秒
        /// </summary>
        public long CacheValidPeriod { get; set; }
        /// <summary>
        /// 超时时间，单位毫秒
        /// </summary>
        public long Timeout { get; set; }
        /// <summary>
        /// 是否可以编辑，针对CDO类型的Action是只读不可编辑的
        /// </summary>
        public bool IsEditable { get; set; }
        /// <summary>
        /// 是否分页
        /// </summary>
        public bool IsEnablePaging { get; set; }
        /// <summary>
        /// 是否支持动态条件
        /// </summary>
        public bool IsEnableDynamicFilter { get; set; }
        /// <summary>
        /// 动态条件对应的元数据ID，比如EO的ID
        /// </summary>
        public string DynamicFilterSourceID { get; set; }
        /// <summary>
        /// 动态条件对应的元数据名称，比如EO的名称
        /// </summary>
        public string DynamicFilterSourceName { get; set; }
        /// <summary>
        /// 是否支持动态排序
        /// </summary>
        public bool IsEnableDynamicSort { get; set; }
        /// <summary>
        /// 动态排序对应的元数据ID，比如EO的ID
        /// </summary>
        public string DynamicSortSourceID { get; set; }
        /// <summary>
        /// 动态排序对应的元数据名称,比如EO的名称
        /// </summary>
        public string DynamicSortSourceName { get; set; }
        /// <summary>
        /// 参数映射脚本
        /// </summary>
        public string ParamMappingJS { get; set; }
        /// <summary>
        /// 结果映射脚本
        /// 
        /// </summary>
        public string ResultMappingJS { get; set; }
        /// <summary>
        /// 是否系统默认Action
        /// </summary>
        /// <remarks>
        /// 系统默认提供针对CDO的Action操作，每一个CDO的Action是默认CDO Action的引用
        /// </remarks>
        public bool IsSysDefault { get; set; }
        /// <summary>
        /// 是否启用数据权限，默认为false：不启用
        /// </summary>
        public bool IsPermissionControl { get; set; }
        /// <summary>
        /// Action的ACL
        /// </summary>
        public ACLDefine ActionACLDefine { get; set; }
        /// <summary>
        /// Action权限控制设置信息
        /// </summary>
        //public ActionPermissionDefinitionForView ActionPermission { get; set; }

        /// <summary>
        /// 关键应用ID
        /// </summary>
        public string AppID { get; set; }
        /// <summary>
        /// 开发模块ID
        /// </summary>
        public string ModuleID { get; set; }
        /// <summary>
        /// 租户ID
        /// </summary>
        public string TenantID { get; set; }
        public string Ext1 { get; set; }
        public string Ext2 { get; set; }
        public string Ext3 { get; set; }

        public DateTime CreateTime { get; set; }
        public string Creator { get; set; }
        public DateTime LastModifyTime { get; set; }
        public string LastModifier { get; set; }

        public List<ActionMetaDataMethodParam> MethodParams { get; set; }

        public List<ActionMetaDependency> MetaDependencys { get; set; }
        public ActionMetaDataMethod Method { get; set; }
        #region 元数据公共基础属性
        public string MetaDataID { get; set; }
        public string MetaDataCode { get; set; }
        public string MetaDataName { get; set; }
        
        [JsonIgnore]
        public IMetaData MetaData { get; set; }
        //public Version CurrentVersion { get; set; }
        //public Version CompatibleMinVersion { get; set; }
        //public Version EngineVersion { get; set; }
        public string ProjectBranchID { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public object Clone()
        {
            var newObject = this.MemberwiseClone() as ActionMetaData;

            return newObject;
        }

        #endregion

        public void ActionSchemaRefresh(TSchema schema)
        {
            if (schema != null)
            {
                schema.ActionID = this.ID;
                schema.ActionCode = this.Code;
                schema.ActionName = this.Name;
                if (this.ReturnValueType == ReturnValueType.List)
                {
                    schema.ResultType = DataType.List;
                    if(schema.Entity!=null)
                        schema.Entity.ResultType = DataType.List;
                }
                else if (this.ReturnValueType == ReturnValueType.Record)
                {
                    schema.ResultType = DataType.Record;
                    if(schema.Entity!=null)
                        schema.Entity.ResultType = DataType.Record;
                }
            }
        }

        public void RefreshID(string newActionMetaDataID)
        {
            this.ID = newActionMetaDataID;
            if(ActionACLDefine!=null)
                ActionACLDefine.ACLMetaDataID = newActionMetaDataID;
            if (Method != null)
            {
                Method.ActionMetaDataID = newActionMetaDataID;
            }
            if (MethodParams != null && MethodParams.Count > 0)
            {
                MethodParams.ForEach(p =>
                p.ActionMetaDataID = newActionMetaDataID);
            }
            //if (ActionPermission != null)
            //{
            //    ActionPermission.ActionID = newActionMetaDataID;
            //}
            
        }

        public bool? IsSync { get; set; } 
    }
}
